<?php

declare(strict_types=1);

namespace app\common\dao\crm;

use app\common\model\crm\CrmCustomer;
use app\common\traits\DaoTrait;
use think\facade\Db;

/**
 * 客户表 数据访问层(Dao)
 * @author 吕玉冰
 * @date  2022-07-21
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class CustomerDao
{
    // 当前操作的模型
    protected $model = null;

    // 对应模型的主键，默认为id
    protected $pk = 'id';

    // 对应模型默认查询的字段
    protected $name = '';

    // 对应模型允许查询的字段(默认全部查询)
    protected $allowField = ['*'];

    // 允许单字段更新
    protected $uaField = [''];

    // 查询结果-要隐藏的字段 默认空数组
    protected $hideField = [];

    public function __construct()
    {
        $this->model = new CrmCustomer();
    }

    use DaoTrait;


    public function getAddNumberByWeek($user_ids)
    {
        $where = [];
        $where[] = ['owner_user_id', 'in', $user_ids];
        $where[] = ['parent_id', '<>', 0];
        $start = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
        $end = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
        return $this->model->where($where)->whereBetweenTime('create_time', $start, $end)->count();
    }

    public function getAddNumberByWeekOpen($user_ids)
    {
        $where = [];
        $where[] = ['owner_user_id', 'in', $user_ids];
        $where[] = ['parent_id', '=', 0];
        $start = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
        $end = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));
        return $this->model->where($where)->whereBetweenTime('create_time', $start, $end)->count();
    }


    /**
     * lvyubing
     * @param $id
     * @return array
     */
    public function getRecordContractById($id)
    {
        $data = [];
        $where = [];
        $where[] = ['relation_type', '=', '1'];
        $where[] = ['relation_id', '=', $id];
        $where[] = ['delete_time', '=', null];
        $data['follow_time'] = Db::name('crm_record')->where($where)->order(["follow_time" => "desc"])->value("follow_time");
        if (!empty($data['follow_time'])) {
            $data['follow_date'] = date('Y-m-d', $data['follow_time']);
        } else {
            $data['follow_time'] = NUll;
        }
        $where = [];
        $where[] = ['customer_id', '=', $id];
        $where[] = ['delete_time', '=', null];
        $data['order_date'] = Db::name('crm_contract')->where($where)->order(["order_date" => "desc"])->value("order_date");
        if (!empty($data['order_date'])) {
            $data['order_time'] = strtotime($data['order_date']);
        } else {
            $data['order_date'] = NUll;
        }
        return $data;
    }

    /**
     * lvyubing
     * @param $user_ids
     * @return array
     */
    public function getSourceByMonth($user_ids)
    {
        $where = [];
        $where[] = ['owner_user_id', 'in', $user_ids];
        $month = date("Y-m");
        $end = date("Y-m-d", strtotime("+1 month -1 day", strtotime($month)));
        $start = date("Y-m-d", strtotime("-1 year", strtotime($month)));
        $data = $this->model->field("count(id) as c,FROM_UNIXTIME(create_time,'%Y-%m') as month,source")->where($where)->whereBetweenTime('create_time', $start, $end)->group("source,month")->select()->toArray();
        $re = [];
        foreach ($data as $k => $v) {
            $re[$v['source']][$v['month']] = $v["c"];
        }
        return $re;
    }

}